<?php

namespace App\Http\Controllers\Api;

use App\Models\RedeemLog;
use Illuminate\Http\Request;
use App\Http\Requests\Api\RedeemLogRequest;
use App\Transformers\RedeemLogTransformer;

class RedeemLogsController extends Controller
{
    public function search(Request $request, RedeemLog $redeemLog)
    {
        $this->authorize('update', $redeemLog);
        $per_page = $request->input('per_page', 20);
        $query = $redeemLog->query();
        if ($prize_id = $request->prize_id) {
            $query=$query->where('prize_id', $request->prize_id);
        }

        if ($user_id = $request->user_id) {
            $query=$query->where('user_id', $request->user_id);
        }

        if ($company_userid = $request->company_userid) {
            $query=$query->where('company_userid', $request->company_userid);
        }

        if ($filiale_id = $request->filiale_id) {
            $query=$query->where('filiale_id', $request->filiale_id);
        }

        if (($redeem_begin = $request->redeem_begin) && ($redeem_end = $request->redeem_end)) {
            $query=$query->where('created_at', '>=', $request->redeem_begin);
            $query=$query->where('created_at', '<=', $request->redeem_end);
        }

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $redeemLogs = $query->paginate($per_page);

        return $this->response->paginator($redeemLogs, new RedeemLogTransformer());
    }

	public function index(Request $request, RedeemLog $redeemLog)
    {
        $this->authorize('update', $redeemLog);
        $per_page = $request->input('per_page', 20);
        $query = $redeemLog->query();

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $redeemLogs = $query->paginate($per_page);

        return $this->response->paginator($redeemLogs, new RedeemLogTransformer());
    }

    public function managerIndex(Request $request)
    {
        $this->authorize('update', $redeemLog);
        $per_page = $request->input('per_page', 20);

        $redeemLogs=null;

        if ($orderBy = $request->orderBy) {
            $redeemLogs = $this->user->managerRedeemLogs()->ordered()
                ->paginate($per_page);
        } else {

            $redeemLogs = $this->user->managerRedeemLogs()->recent()
                ->paginate($per_page);
        }

        return $this->response->paginator($redeemLogs, new RedeemLogTransformer());
    }

    public function userIndex(Request $request)
    {
        $this->authorize('update', $redeemLog);
        $per_page = $request->input('per_page', 20);

        $redeemLogs=null;

        if ($orderBy = $request->orderBy) {
            $redeemLogs = $this->user->redeemLogs()->ordered()
                ->paginate($per_page);
        } else {

            $redeemLogs = $this->user->redeemLogs()->recent()
                ->paginate($per_page);
        }

        return $this->response->paginator($redeemLogs, new RedeemLogTransformer());
    }

    public function show(RedeemLog $redeemLog)
    {
        $this->authorize('update', $redeemLog);
        return $this->response->item($redeemLog, new RedeemLogTransformer());
    }

}